package com.eascs.crm.web.controller;

import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.common.service.ILoanInfoService;
import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.crm.common.controller.BaseMasterViewController;
import com.eascs.crm.common.entity.CustomerUnit;
import com.eascs.crm.common.vo.QueryCustomerVO;
import com.eascs.crm.server.manger.impl.CustomerInfoManagerImpl;
import com.eascs.crm.web.service.LocalCustomerInfoServiceImpl;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.web.url.UrlHelper;
import com.eascs.workflow.common.entity.sys.ActOrgEntity;
import com.eascs.workflow.common.service.sys.SysOrgService;

@Controller
@RequestMapping("/customer")
public class CustomerController extends BaseMasterViewController {
	private final Logger logger = LoggerFactory.getLogger(CustomerInfoManagerImpl.class);

	@Inject
	LocalCustomerInfoServiceImpl customerInfoService;
	@Reference
	SysOrgService sysOrgService;
	@Reference
	ILoanInfoService iLoanInfoService;

	@RequestMapping(value = "/getCustomerBasicInfo")
	public String getCustomerBasicInfoEntity(QueryCustomerVO vo) {
		return "/customer/index";

	}

	@RequestMapping(value = "/add")
	public String add() {
		return "/customer/add";

	}

	// 跳转客户管理信息界面
	@RequestMapping(value = "/toCustomerManageInfo")
	public String toCustomerManageInfo(String customerId) {
		ViewContext.put("customerId", customerId);
		return "/customer/customerManageInfo";
	}

	// 平台信息
	@RequestMapping(value = "/toPlatformInfo")
	public String toPlatformInfo(String customerId) {
		// 根据客户id查询平台信息
		List<CustomerUnit> list = customerInfoService.getCustomerUnitInfo(customerId);
		// 根据客户id查询平台个数
		String count = customerInfoService.getUnitCount(customerId);
		String flag = "1";
		if (Integer.valueOf(count) <= 1) {
			flag = "0";
		}
		ViewContext.put("vo", list);
		ViewContext.put("customerId", customerId);
		ViewContext.put("flag", flag);
		return "/customer/platformInfo";
	}

	// 添加平台信息
	@RequestMapping(value = "/addPlatformInfo")
	public String addPlatformInfo(QueryCustomerVO vo, String customerId) {
		// 平台关联
		ViewContext.put("unitInfoUrl", UrlHelper.build("/common/searchUnit?org_name="));
		ViewContext.put("customerId", customerId);
		ViewContext.put("vo", vo);
		return "/customer/platformInfo_add";
	}

	// 删除平台
	@RequestMapping(value = "/del_unit")
	public String delUnit(String unitId, String customerId) {
		// 根据客户id查询平台个数
		String count = customerInfoService.getUnitCount(customerId);
		String flag = "1";
		if (Integer.valueOf(count) <= 2) {
			flag = "0";
		}
		if (StringUtil.isNotBlank(unitId)) {
			try {
				// 根据平台id和客户id删除平台
				customerInfoService.delUnit(unitId, customerId);
			} catch (Exception e) {
				logger.info("删除失败，客户id：[{}]，平台id：[{}]", unitId, customerId);
				e.printStackTrace();
			}
		}
		// 根据客户id查询平台
		List<CustomerUnit> list = customerInfoService.getCustomerUnitInfo(customerId);
		ViewContext.put("vo", list);
		ViewContext.put("customerId", customerId);
		ViewContext.put("flag", flag);
		return "/customer/platformInfo";
	}

	// 保存平台信息
	@RequestMapping(value = "/unit_save")
	public String saveUnit(QueryCustomerVO vo, String customerId) {
		if (StringUtil.isNotBlank(vo.getUnit_id())) {
			// 根据平台id查询平台信息
			ActOrgEntity actOrgEntity = sysOrgService.getOrgByUnitNo(vo.getUnit_id());
			if (actOrgEntity != null) {
				vo.setUnit_id(actOrgEntity.getUnitNo());
				vo.setPageSize(10);
				// 根据客户id和平台id查询该客户已有的平台
				PageResult<Object[]> p = customerInfoService.findCustomerUnitInfo(customerId, vo.getUnit_id());
				if (p != null && !p.getData().isEmpty()) {
					ViewContext.put("customerId", customerId);
					return "/customer/platformRepeat";
				}
				CustomerUnit c = new CustomerUnit();
				c.setUnitId(actOrgEntity.getUnitNo());
				c.setUnitCode(actOrgEntity.getUnitCode());
				c.setUnitName(actOrgEntity.getOrgFullName());
				c.setCustomerId(customerId);
				c.setOpTime(new Date());
				try {
					// 保存平台信息
					customerInfoService.addCustomerUnit(c);
					logger.info("保存平台成功，平台id：[{}]", vo.getUnit_id());
				} catch (Exception e) {
					logger.info("保存平台失败，平台id：[{}]", vo.getUnit_id());
					e.printStackTrace();
				}
			} else {
				logger.info("该平台不存在，平台id：[{}]", vo.getUnit_id());
				BaseRestResult.warn("平台不存在");
			}
		}else{
			ViewContext.put("customerId", customerId);
			return "/customer/platformInfoNotNull";
		}
		String count = customerInfoService.getUnitCount(customerId);
		String flag = "1";
		if (Integer.valueOf(count) <= 1) {
			flag = "0";
		}
		List<CustomerUnit> list = customerInfoService.getCustomerUnitInfo(customerId);
		ViewContext.put("vo", list);
		ViewContext.put("customerId", customerId);
		ViewContext.put("flag", flag);
		return "/customer/platformInfo";
	}
}
